package com.autumn.aitraffic.controller;


import com.autumn.aitraffic.api.Result;
import com.autumn.aitraffic.domain.po.Driver;
import com.autumn.aitraffic.service.IDriverService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wzx
 * @since 2025-01-22
 */
@RestController
@RequestMapping("/personal/driver")
@CrossOrigin(origins = "*")
public class DriverController {
    @Autowired
    IDriverService driverService;

    @GetMapping("list")
    public Result driverList(@RequestParam int pageNum, @RequestParam int pageSize, @RequestParam(required = false) String name
            , @RequestParam(required = false) String phoneNumber, @RequestParam(required = false) String plateNumber){
        Driver driver = new Driver();
        if(name!=null&&!"".equals(name)){
            driver.setUsername(name);
        }
        if (phoneNumber!=null&&!"".equals(phoneNumber)){
            driver.setTelephone(phoneNumber);
        }
        if (plateNumber!=null&&!"".equals(plateNumber)){
            driver.setPlatenum(plateNumber);
        }
        Page page = new Page(pageNum, pageSize);
        return driverService.pagequery(page,driver);
    }
    @PostMapping("add")
    public Result addDriver(@RequestBody Driver driver){
        driverService.save(driver);
        return Result.succ(null,null);
    }
    @PutMapping("update")
    public Result updateDriver(@RequestBody Driver driver){
        return driverService.updateDriver(driver);
    }
    @DeleteMapping("delete/{id}")
    public Result deleteDriver(@PathVariable int id){
        driverService.removeById(id);
        return Result.succ(null,null);
    }
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        driverService.removeByIds(ids);
        return Result.succ(null,null);
    }
    @PostMapping("importData")
    public Result importDriverData(@RequestBody MultipartFile file){
        return driverService.importDriverData(file);
    }
    @GetMapping("export")
    public Result exportDriverData(){
        return driverService.exportDriverData();
    }
}
